package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssClient;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author leo
 * @since 2021-11-24
 */
@RestController
@Api(tags = "讲师管理模块") //@Api描述controller
@RequestMapping("/admin/edu/teacher")
@Slf4j
 //允许跨域访问
public class AdminTeacherController {
    @Autowired
    private TeacherService teacherService ;
    @Autowired
    private OssClient ossClient ;
    //1.查询所有讲师
    @ApiOperation(value = "查询所有讲师") //@ApiOperation描述接口
    @GetMapping("list")
    public R queryAll() {
        /*//{} 代表占位符
        log.debug("{}级别日志" , "debug");
        log.info("{}级别日志" , "info");
        log.warn("{}级别日志" , "warn");
        log.error("{}级别日志" , "error");*/
        List<Teacher> teacherList = teacherService.list();
        R r = ossClient.test("Taylor", "1988loveme");
        log.error(r.getMessage());
        R test2 = ossClient.test2(R.setResult(ResultCodeEnum.UNKNOWN_REASON));
        log.error(test2.getMessage());
        return R.ok().data("items",teacherList) ;
    }

    //2.新增讲师
    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@RequestBody Teacher teacher) {
        if (teacherService.save(teacher)) {
            return R.ok();
        }
        return R.error() ;
    }

    //3.更新讲师
    @ApiOperation("根据id查找讲师")
    @GetMapping("getById/{id}")
    public R getTeacher(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher) ;
    }
    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R update(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher) ;
        return R.ok().message("更新成功") ;
    }

    //4.删除讲师
    @ApiOperation("删除讲师")
    @DeleteMapping("delete/{id}")
    public R delTeacher(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        boolean result = teacherService.removeById(id);//先删除讲师
        if (result && StringUtils.isEmpty(teacher.getAvatar())) {
            ossClient.delete(teacher.getAvatar(),"avatar") ;            //再删除讲师头像
        }
        return R.ok().message("删除成功") ;
    }

    //5.带条件的分页查询
    @ApiOperation(value = "带条件分页查询讲师列表")
    @GetMapping("list/{pageNum}/{pageSize}") //@ApiParam描述参数
    public R queryByPage(@ApiParam(value = "页码",defaultValue = "1",required = true) @PathVariable Integer pageNum,
                         @ApiParam(value = "当前页记录条数",defaultValue = "3",required = true) @PathVariable Integer pageSize,
                         TeacherQuery teacherQuery) {
        IPage<Teacher> page = new Page<>(pageNum,pageSize) ;
        teacherService.getPageByCondition(page,teacherQuery) ;
        return R.ok().data("page",page) ;
    }

    //6.批量删除讲师
    @ApiOperation(value = "批量删除讲师")
    @DeleteMapping("batchDel")
    public R batchDel(@RequestBody List<String> ids) {
        if (ids.size() > 0) {
            teacherService.removeByIds(ids) ;
        }
        return R.ok().message("批量删除成功") ;
    }

    /**
     * @GetMapping不能和@RequestBody一起使用，因为get请求没有请求体
     * @RestController和@Controller区别：
     *      1.@RestController注解相当于@ResponseBody ＋ @Controller合在一起的作用，
     *      2.如果只是使用@RestController注解Controller，则Controller中的方法无法返回jsp页面，
     *        或者html，配置的视图解析器 InternalResourceViewResolver不起作用，返回的内容就是Return里的内容
     *      3.如果需要返回到指定页面，则需要用 @Controller配合视图解析器InternalResourceViewResolver才行
     *      4.如果需要返回JSON，XML或自定义mediaType内容到页面，则需要在对应的方法上加上@ResponseBody注解
     */

}

